import {
  Controller,
  Post,
  Body,
  UploadedFile,
  UseInterceptors,
} from '@nestjs/common';
import { PlayerSignupService } from '../service/player-signup.service';
import { FileInterceptor } from '@nestjs/platform-express';
import { ApiConsumes, ApiBody, ApiBearerAuth } from '@nestjs/swagger';
import logger from 'src/common/utils/log';
import { ConfirmPlayerE2PService } from '../../event-to-player/services/confirmPlayerE2P.service';
import { CreateSignupService } from '../service/createSignup';
import { PlayerSignup } from '../entities/player-signup.entity';
import { Auth } from 'src/common/decorators/auth.decorator';
import { RequirePermission } from 'src/common/decorators/permission.decorator';

@Auth()
@ApiBearerAuth()
@Controller('player-signup/upload')
export class PlayerSignupUploadController {
  constructor(
    private readonly playerSignupService: PlayerSignupService,
    private readonly confirmPlayerE2PService: ConfirmPlayerE2PService,
    private readonly createSignupService: CreateSignupService,
  ) {}
  @RequirePermission('1002002')
  @Post('uploadPlayers')
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        file: {
          type: 'string',
          format: 'binary',
        },
        tournamentId: {
          type: 'string',
        },
      },
    },
  })
  @UseInterceptors(FileInterceptor('file'))
  async uploadPlayerSignupInfo(
    @UploadedFile() file: Express.Multer.File,
    @Body() params: { tournamentId: number },
  ) {
    const { tournamentId } = params;
    logger.info(`start uploadPlayers ${tournamentId}`);
    const playerSignupInfo =
      await this.createSignupService.getPlayerSignupFromExcel(
        tournamentId,
        file,
      );
    const savedList = await this.playerSignupService.saveList(
      playerSignupInfo as PlayerSignup[],
    );
    logger.info(`uploadPlayerSignupInfo save ${savedList.length} success`);
  }
  @RequirePermission('1002002')
  @Post('uploadPlayersAndConfirmE2P')
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        file: {
          type: 'string',
          format: 'binary',
        },
        tournamentId: {
          type: 'string',
        },
        stageIndex: {
          type: 'number',
          default: 0,
        },
      },
    },
  })
  @UseInterceptors(FileInterceptor('file'))
  async uploadPlayersAndConfirmE2P(
    @UploadedFile() file: Express.Multer.File,
    @Body() params: { tournamentId: number; stageIndex: number },
  ) {
    // 和 uploadPlayerSignupInfo 一样 st
    const { tournamentId, stageIndex } = params;
    logger.info(`start uploadPlayers ${tournamentId}`);
    const playerSignupInfo =
      await this.createSignupService.getPlayerSignupFromExcel(
        tournamentId,
        file,
        stageIndex
      );
    const savedList = await this.playerSignupService.saveList(
      playerSignupInfo as PlayerSignup[], /// 有错误，subplayer 没有第一个数据没有statgeeventid
    );
    logger.info(`uploadPlayerSignupInfo save ${savedList.length} success`);
    // 和 uploadPlayerSignupInfo 一样 ed
    logger.info('start ConfirmPlayerE2P');
    // 将playerSignupInfo 按照eventId 分组
    const confirmE2PParamList =
      this.createSignupService.getConfirmE2PParam(savedList);

    await Promise.all(
      confirmE2PParamList.map((confirmE2PParam) => {
        return this.confirmPlayerE2PService.confirmPlayerE2P(
          confirmE2PParam[0],
          'custom',
          confirmE2PParam[1],
          parseInt(stageIndex.toString()),
        );
      }),
    );
  }
}
